<template>
  <div class="airAccount">
    <el-form :model="thequery" ref="thequery" size="mini" style="margin: 0 0 20px 10px">
      <div style="display: flex">
        <el-form-item label="航空公司" style="display: flex; margin: 0 20px 0 0">
          <el-select filterable clearable v-model="thequery.airlinerId" placeholder="请选择">
            <el-option v-for="item in airportLists" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="账号所属城市" style="display: flex; margin: 0 20px 0 0">
          <el-select clearable v-model="thequery.address" filterable style="width: 150px" placeholder="请选择">
            <el-option v-for="(item, index) in querySearch" :key="index" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="B2B网站登录名" style="display: flex; margin: 0 20px 0 0">
          <el-input v-model="thequery.loginid" size="mini"></el-input>
        </el-form-item>
        <el-form-item style="margin: 0 20px 0 50px">
          <el-button type="primary" size="mini" @click="searchBtn">查询</el-button>
          <el-button type="primary" size="mini" @click="newairPort">新增</el-button>
        </el-form-item>
      </div>
    </el-form>
    <!-- 用户列表区 -->
    <el-table :header-cell-style="{
        color: '#fff',
        background: '#6090EC',
        fontSize: '11px',
        fontWeight: 'normal',
      }" :data="auxiliary" size="mini" border stripe>
      <el-table-column label="航空公司" prop="airlinerId" width="180"></el-table-column>
      <!-- <el-table-column label="账号所属城市" prop="address">

      </el-table-column> -->
      <el-table-column label="登录账号" prop="loginid"></el-table-column>
      <el-table-column label="登录密码">
        <template slot-scope="scope">
          <div>{{Substringed(scope.row.password)}}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" prop="employee">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="editairAccomethod(scope.row)">修改</el-button>

          <el-button slot="reference" type="primary" @click="deleteairPort(scope.row)" size="mini">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增 -->
    <el-dialog title="网点账号" :visible.sync="dialogVisible" :close-on-click-modal="false" size="mini" width="50%">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" size="mini" class="demo-ruleForm">
        <div style="display: flex">
          <el-form-item label="航空公司" prop="name" style="display: flex; margin: 0 50px 0 0">
            <el-select clearable v-model="ruleForm.name" placeholder="请选择">
              <el-option v-for="item in airportLists" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="登录账号" prop="account" style="display: flex">
            <el-input v-model="ruleForm.account"></el-input>
          </el-form-item>
        </div>
        <div style="display: flex">
          <el-form-item label="登录密码" prop="pass" style="display: flex; margin: 0 65px 0 0">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPass" style="display: flex">
            <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
          </el-form-item>
        </div>
        <!-- <el-form-item label="航空公司附件" prop="attachment" style="display:flex;margin:0 65px 0 0">
              <el-input style="width:500px;" v-model="ruleForm.attachment"></el-input>
         </el-form-item> -->
        <!-- <el-form-item label="账号所属城市" prop="city" style="display: flex; margin: 15px 65px 20px 0">
          <el-select clearable v-model="ruleForm.city" filterable style="width: 500px" placeholder="请选择">
            <el-option v-for="(item, index) in querySearch" :key="index" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="注意：" style="display: flex; margin: 15px 65px 20px 0">
          <!-- <div style="color: red; font-size: 12px">
            (1) 如多个城市请选择多个城市
          </div> -->
          <div style="color: red; font-size: 12px">
            (1) 如果账号使用所有城市，请保持为空
          </div>
        </el-form-item>
        <el-form-item style="margin: 0 0 0 70%">
          <el-button v-if="!ruleForm.accountId" type="primary" @click="gocreate()">立即创建</el-button>
          <el-button v-if="ruleForm.accountId" type="primary" @click="gocreate()">确认修改</el-button>
          <el-button type="" @click="dialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 删除弹出框 -->
    <el-dialog title="删除" :visible.sync="delDialogVisible" :close-on-click-modal="false" width="30%" center>
      <span>确认删除此条账号吗</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="getdeleteairaccount()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 分页 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
      :page-sizes="[10, 15, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
      :total="pageTotal">
    </el-pagination>
  </div>
</template>

<script>
  export default {
    name: "airAccount",
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === "") {
          callback(new Error("请输入密码"));
        } else {
          if (this.ruleForm.checkPass !== "") {
            this.$refs.ruleForm.validateField("checkPass");
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === "") {
          callback(new Error("请再次输入密码"));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error("两次输入密码不一致!"));
        } else {
          callback();
        }
      };
      return {
        // 校验
        rules: {
          name: [{
            required: true,
            message: "请选择航空公司",
            trigger: "change"
          }, ],
          account: [{
            required: true,
            message: "请输入登录账号",
            trigger: "blur"
          }, ],
          attachment: [{
            required: true,
            message: "请输入附件",
            trigger: "blur"
          }, ],
          city: [{
            required: true,
            message: "选择账号所属城市",
            trigger: "change"
          }, ],
          pass: [{
            validator: validatePass,
            trigger: "blur"
          }],
          checkPass: [{
            validator: validatePass2,
            trigger: "blur"
          }],
        },
        // 新增
        ruleForm: {
          name: "", //航司三字码
          pass: "", //密码
          account: "", //登录账号
          city: "", //地址
          accountId: "",
          checkPass:""
        },
        // 弹窗
        dialogVisible: false,
        // 删除弹窗
        delDialogVisible: false,
        // 删除id
        delid: "",
        // 城市
        querySearch: [],
        //航空公司
        airportLists: [],
        // 搜索条件
        thequery: {
          address: "", //地址
          airlinerId: "", //航司
          loginid: "", //登录名
        },
        // 表格数组
        auxiliary: [],
        // 分页
        total: 0,
        pageSize: 10,
        currentPage: 1,
        pageTotal: 0,
      };
    },
    methods: {
      //处理密码展示
      Substringed(identity) {
        var ident = identity.replace(identity.substring(0, 20), "*********")
        return ident
      },
      // 删除按钮
      deleteairPort(row) {
        this.delDialogVisible = true;
        console.log(row.accountId);
        this.delid = row.accountId;
      },
      // 修改按钮点击事件
      editairAccomethod(row) {
        console.log(row)
        // this.ruleForm.account = row.loginid
        // this.ruleForm.city = row.address
        // this.ruleForm.name = row.airlinerId
        // this.ruleForm.accountId = row.accountId
        // this.ruleForm.pass = row.password
        // this.ruleForm.checkPass = row.password
        this.dialogVisible = true;
        this.$axios.getairaccountInfo({
          accountId:row.accountId
        }).then((res)=>{
          if(res.code==200){
             this.ruleForm.account = row.loginid
        // this.ruleForm.city = row.address
        this.ruleForm.name = row.airlinerId
        this.ruleForm.accountId = row.accountId
        this.ruleForm.pass = row.password
        // this.ruleForm.checkPass = row.password
          }
        })
      },

      // 新增按钮
      newairPort() {
        this.ruleForm.accountId = ''
        this.ruleForm.city = ""
        this.ruleForm.account = ""
        this.ruleForm.name = ""
        this.ruleForm.pass = ""
        this.ruleForm.checkPass = ''
        this.dialogVisible = true;
      },
      // 城市
      getselectCityList() {
        this.$axios.getselectCityList({}).then((res) => {
          console.log(res);
          if (res.code == "200") {
            let list = JSON.parse(JSON.stringify(res.data));
            this.querySearch = list.map((item) => {
              return {
                label: item.cityName,
                value: item.cityName
              };
            });
            console.log(this.querySearch);
          }
        });
      },
      // 航空公司列表
      getairportList() {
        localStorage.removeItem("pageNum");
        localStorage.removeItem("pageSize");
        this.$axios.getqueryAirlinerSelect({}).then((res) => {
          localStorage.setItem("pageSize", 10);
          localStorage.setItem("pageNum", 1);
          console.log(res);
          if (res.code == "200") {
            let list = JSON.parse(JSON.stringify(res.data));
            this.airportLists = list.map((item) => {
              return {
                label: item.airliner,
                value: item.airlinerId
              };
            });
            console.log(this.airportLists);
          }
        });
      },
      //分页
      handleSizeChange(value) {
        console.log("1", value);
        localStorage.setItem("pageSize", value);
        localStorage.setItem("pageNum", 1);
        this.getairaccountlist();
        this.pageSize = value;
      },
      handleCurrentChange(value) {
        console.log("2", value);

        localStorage.setItem("pageNum", value);
        localStorage.setItem("pageSize", this.pageSize);
        this.currentPage = value;

        this.getairaccountlist();
        // orders = res.data;
      },
      search() {
        localStorage.setItem("pageNum", this.currentPage);
        localStorage.setItem("pageSize", 10);
        this.getairaccountlist();
      },
      // 查询接口
      getairaccountlist() {
        localStorage.setItem("pageNum", this.currentPage);
        localStorage.setItem("pageSize", this.pageSize);
        this.$axios
          .getairaccountlist({
            address: this.thequery.address, //地址
            airlinerId: this.thequery.airlinerId, //航司ID
            loginid: this.thequery.loginid, //登录名
          })
          .then((res) => {
            console.log(res);
            if (res.code == "200") {

              this.pageTotal = res.total;
              this.auxiliary = res.data;
            }
          });
      },
      // 新增接口
      getaddairaccount() {
        if (
          // this.ruleForm.city != "" &&
          this.ruleForm.account != "" &&
          this.ruleForm.name != "" &&
          this.ruleForm.pass != ""
        ) {
          this.$axios
            .getaddairaccount({
              address: this.ruleForm.city, //地址
              loginid: this.ruleForm.account, //账号airlinerId
              airlinerId: this.ruleForm.name, //航司
              password: this.ruleForm.pass, //密码
            })
            .then((res) => {
              console.log(res);
              if (res.code == "200") {
                this.dialogVisible = false;
                this.ruleForm = {}

                this.getairaccountlist();
                this.$message({
                  message: "新增成功",
                  type: "success",
                });
              }
            });
        } else {
          this.$message({
            message: "请认真填写，所有选项均为必填项",
            type: "warning",
          });
        }
      },
      searchBtn() {
        localStorage.setItem("pageNum", 1);
        localStorage.setItem("pageSize", 10);
        this.getairaccountlist();
      },
      // 修改接口i
      getairaccountupdate() {
        if (
          // this.ruleForm.city != "" &&
          this.ruleForm.account != "" &&
          this.ruleForm.name != "" &&
          this.ruleForm.pass != ""
        ) {
          this.$axios
            .getairaccountupdate({
              address: this.ruleForm.city, //地址
              loginid: this.ruleForm.account, //账号airlinerId
              airlinerId: this.ruleForm.name, //航司
              password: this.ruleForm.pass, //密码
              accountId: this.ruleForm.accountId //主键id
            })
            .then((res) => {
              console.log(res);
              if (res.code == "200") {
                this.dialogVisible = false;
                this.getairaccountlist();
                this.$message({
                  message: "修改成功",
                  type: "success",
                });
                this.ruleForm = {}
              }
            });
        } else {
          this.$message({
            message: "请认真填写，所有选项均为必填项",
            type: "warning",
          });
        }
      },
      gocreate() {
        if (this.ruleForm.accountId == "") {

          this.getaddairaccount()
        } else {
          this.getairaccountupdate()
        }
      },
      // 删除接口
      getdeleteairaccount() {
        this.$axios
          .getdeleteairaccount({
            accountId: Number(this.delid),
          })
          .then((res) => {
            console.log(res);
            if (res.code == "200") {
              this.delDialogVisible = false;
              this.getairaccountlist();
              this.$message({
                message: "删除成功",
                type: "success",
              });
            }
          });
      },
    },
    mounted() {
      // 航空公司
      this.getairportList();
      // 城市
      this.getselectCityList();
      // 列表查询
      this.getairaccountlist();
    },
  };
</script>

<style scoped>
  .airAccount {
    margin: 20px;
  }
</style>